

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/IKKIS_LOGO.png">
  <link rel="icon" href="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/IKKIS_LOGO.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Ikki">
  <meta name="keywords" content="">
  
    <meta name="description" content="进制转换是人们利用符号来计数的方法。进制转换由一组数码符号和两个基本因素“基数”与“位权”构成。基数是指，进位计数制中所采用的数码（数制中用来表示“量”的符号）的个数。位权是指，进位制中每一固定位置对应的单位值。">
<meta property="og:type" content="article">
<meta property="og:title" content="进制转换">
<meta property="og:url" content="http://81.68.132.127/2022/10/20/JS/binary/%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="IKKI">
<meta property="og:description" content="进制转换是人们利用符号来计数的方法。进制转换由一组数码符号和两个基本因素“基数”与“位权”构成。基数是指，进位计数制中所采用的数码（数制中用来表示“量”的符号）的个数。位权是指，进位制中每一固定位置对应的单位值。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/beijing/iTab-4vzpyp.jpeg">
<meta property="article:published_time" content="2022-10-19T23:16:20.000Z">
<meta property="article:modified_time" content="2022-10-19T23:28:45.919Z">
<meta property="article:author" content="Ikki">
<meta property="article:tag" content="前端基础">
<meta property="article:tag" content="js">
<meta property="article:tag" content="进制转换">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/beijing/iTab-4vzpyp.jpeg">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>进制转换 - IKKI</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/fluid/css/fluid-extention.css">
<link rel="stylesheet" href="/fluid/css/nihao.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"81.68.132.127","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":3},"lazyload":{"enable":true,"loading_img":"https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 100vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Ikki-BUG制造者</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('http://image.ikki0603.cn') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="进制转换"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-10-20 07:16" pubdate>
          2022年10月20日 早上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.7k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          31 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
        <div class="scroll-down-bar">
          <i class="iconfont icon-arrowdown"></i>
        </div>
      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">进制转换</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2022年10月20日 早上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p><a target="_blank" rel="noopener" href="http://zhufengpeixun.com/strong/html/104.1.binary.html">链接</a></p>
<h1 id="1-计算机中的数值表示"><a href="#1-计算机中的数值表示" class="headerlink" title="1.计算机中的数值表示"></a>1.计算机中的数值表示</h1><ul>
<li>进位计数制两大要素<ol>
<li>基数R</li>
<li>位权W<h2 id="1-1-基数R"><a href="#1-1-基数R" class="headerlink" title="1.1 基数R"></a>1.1 基数R</h2></li>
</ol>
</li>
<li>R代表基本数码的个数</li>
<li>R进制的主要特点是逢R进1</li>
<li>基数R的数制称为R进制数</li>
</ul>
<p>R |    进制    |数码符号|    进制规则<br>|—–|—–|—–|—–|<br>R=2    |二进制数(2)    |0、1    |逢2进1<br>R=10|    十进制数(10)|    0 ~ 9    |逢10进1</p>
<h2 id="1-2-位权Wi"><a href="#1-2-位权Wi" class="headerlink" title="1.2 位权Wi"></a>1.2 位权Wi</h2><ul>
<li>位权Wi是指第i位上的数码的权重值,位权与数码所处的位置i有关</li>
<li>例如 R=10(十进制数),各个数码的权为10^i, i表示数码所处的位置</li>
<li>个位i=0,位权是10^0=1</li>
<li>十位i=1,位权是10^1=10</li>
<li>(22.22)<del>10</del>=2x10^1^ + 2x10^0^ + 2x10^-1^ + 2x10^-2^</li>
</ul>
<p>R|进制|数码符号|进制规则|位权Wi    |例子<br>|—–|—–|—–|—–|—–|—–|<br>R=2|    二进制数(2)|    0、1|    逢2进1|    2|    (11.11)<del>2</del> = 1x2^1^ + 1x2^0^ + 1x2^-1^ + 1x2^-2^<br>R=10|    十进制数(10)|    0 ~ 9|    逢10进1|    10|    (99.99)<del>10</del> = 9x10^1^ + 9x10^0^ + 9x10^-1^ + 9x10^-2^<br>R=16|    十六进制数(16)|    0 ~ 15|    逢16进1|    16|    (99.99)<del>16</del> = 9x16^1^ + 9x16^0^ + 9x16^-1^ + 9x16^-2^</p>
<h1 id="2-进制转换"><a href="#2-进制转换" class="headerlink" title="2.进制转换"></a>2.进制转换</h1><h2 id="2-1-二进制转十进制-或者-十六进制转十进制"><a href="#2-1-二进制转十进制-或者-十六进制转十进制" class="headerlink" title="2.1 二进制转十进制 或者 十六进制转十进制"></a>2.1 二进制转十进制 或者 十六进制转十进制</h2><ul>
<li>方法: 按权展开,加权求和,以(111.11)<del>2</del>为例<br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10001.1.2to10.gif" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></li>
</ul>
<p>2.2 十进制转二进制</p>
<ul>
<li>整数部分: 除2取余,直到商为0,最先得到的余数是最低位,最后得到的余数是最高位.</li>
<li>小数部分: 乘2取整,直到积为0或者达到精度要求为止,最先得到的整数是高位</li>
<li>例如 (7.75)<del>10</del>=(111.11)<del>2</del><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10002.2to10zheng.gif" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10003.2to10xiaoshu.gif" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></li>
</ul>
<h1 id="3-计算机中的数据"><a href="#3-计算机中的数据" class="headerlink" title="3. 计算机中的数据"></a>3. 计算机中的数据</h1><ul>
<li>数据<ul>
<li>数值数据<ul>
<li>无符号数据</li>
<li>有符号数据(有+-号)</li>
</ul>
</li>
<li>非数值数据<ul>
<li>文字</li>
<li>图像<h2 id="3-1-无符号数据的表示"><a href="#3-1-无符号数据的表示" class="headerlink" title="3.1 无符号数据的表示"></a>3.1 无符号数据的表示</h2></li>
</ul>
</li>
</ul>
</li>
<li>原码: 3个bit(位)能表示8个数 0 1 2 3 4 5 6 7<br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10004.unsigned.gif" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></li>
</ul>
<h2 id="3-2-有符号数据的表示"><a href="#3-2-有符号数据的表示" class="headerlink" title="3.2 有符号数据的表示"></a>3.2 有符号数据的表示</h2><h3 id="3-2-1-原码"><a href="#3-2-1-原码" class="headerlink" title="3.2.1 原码"></a>3.2.1 原码</h3><p>原码: 3个bit能表示8个数 +0 +1 +2 +3 -0 -1 -2 -3<br>符号: 用0、1表示正负号,放在数值的最高位<br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10005.originalcode.gif" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="3-2-2-反码-用来表示负数，用来实现用加法实现减法"><a href="#3-2-2-反码-用来表示负数，用来实现用加法实现减法" class="headerlink" title="3.2.2 反码(用来表示负数，用来实现用加法实现减法)"></a>3.2.2 反码(用来表示负数，用来实现用加法实现减法)</h3><p>反码:正数不变,负数的除符号位外取反<br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10006.completation.gif" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<h3 id="3-2-3-补码（计算中只有补码）"><a href="#3-2-3-补码（计算中只有补码）" class="headerlink" title="3.2.3 补码（计算中只有补码）"></a>3.2.3 补码（计算中只有补码）</h3><p>补码:正数不变,负数在反码的基础上加1 （0 +1 +2 +3 -1 -2 -3 -4）<br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10007.bumacunzai.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload><br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10008.transfromnetoim.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></p>
<blockquote>
<p>例子：-9在计算中的补码是什么?（例如换算成8位）<br>转换成源码：-9 = 100001001<br>转换成反码：-9 = 100001001 = 111110110<br>转换成补码：-9 = 100001001 = 111110110 = 111110111</p>
</blockquote>
<blockquote>
<p>例子2：-3+4 = ？ （例如换算成8位）<br>-3 = 10000011 = 11111100 = 11111101<br>4 = 00000100 = 00000100 = 00000100<br>11111101 + 00000100 = 100000001 =&gt; 00000001 =&gt; 1*2^0 = 1</p>
</blockquote>
<h1 id="4-小数"><a href="#4-小数" class="headerlink" title="4. 小数"></a>4. 小数</h1><h2 id="4-1-小数点表示"><a href="#4-1-小数点表示" class="headerlink" title="4.1 小数点表示"></a>4.1 小数点表示</h2><ul>
<li>在计算机中,小数点及其位置是隐含规定的,小数点并不占用存储空间</li>
<li>定点数: 小数点的位置是固定不变的,分为定点整数(33.)和定点小数(0.33)</li>
<li>浮点数:小数点的位置是会变化的(33.33)<h2 id="4-2-定点小数"><a href="#4-2-定点小数" class="headerlink" title="4.2 定点小数"></a>4.2 定点小数</h2></li>
<li>定点小数: 小数点隐含固定在最高数据位的左边,整数位则用于表示符号位,用于表示纯小数. 例如: (0.110011)2<br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10009.dingdianxiaoshu.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></li>
</ul>
<h2 id="4-3-定点整数"><a href="#4-3-定点整数" class="headerlink" title="4.3 定点整数"></a>4.3 定点整数</h2><ul>
<li>定点整数: 小数点位置隐含固定在最低位之后,最高位为符号位,用于表示纯整数.</li>
<li>例如: (0110011.)2<br><img src="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/Hotchpotch/JS/basic/binary/10010.dingdiaozhengshu.png" srcset="https://ikkiss.oss-cn-hangzhou.aliyuncs.com/images/myblog/loading.gif" lazyload></li>
</ul>
<h2 id="4-4-浮点数（先忽略）"><a href="#4-4-浮点数（先忽略）" class="headerlink" title="4.4 浮点数（先忽略）"></a>4.4 浮点数（先忽略）</h2><ul>
<li>对于既不是定点整数,也不是定点小数的数用浮点数表示</li>
<li>在计算机中通常把浮点数N分成阶码和尾数两部分来表示</li>
<li>小数点位置由阶码规定,因此是浮动的</li>
<li>N=尾数*基数^阶码,基中尾数是一个规格化的纯小数</li>
<li>(3.5)10=(11.1)2=1.11*2^1(1是二进制)</li>
<li>(7.5)10=(111.1)2=1.111*2^10(10是二进制);</li>
<li>科学记数法是一种记数的方法。把一个数表示成a与10的n次幂相乘的形式,可以节约空间和时间</li>
<li>例如:（1≤|a|&lt;10，n为整数）,比如 1100= 1.1x103</li>
</ul>
<h1 id="5-JS转换进制"><a href="#5-JS转换进制" class="headerlink" title="5.JS转换进制"></a>5.JS转换进制</h1><ul>
<li>0b开头的是2进制 </li>
<li>0o开头是8进制</li>
<li>0x开头的是16进制<h2 id="5-1-parseInt-string-radix"><a href="#5-1-parseInt-string-radix" class="headerlink" title="5.1 parseInt(string, radix)"></a>5.1 parseInt(string, radix)</h2></li>
<li>解析一个字符串并返回指定基数的十进制整数， radix 是2-36之间的整数，表示被解析字符串的基数。</li>
</ul>
<figure class="highlight arcade"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-tdvk6glacl5vpl"></i><span>arcade</span><div class="collapse show" id="collapse-tdvk6glacl5vpl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(parseInt(<span class="hljs-string">&#x27;1010&#x27;</span>,<span class="hljs-number">2</span>)); <span class="hljs-comment">// 10 </span><br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(parseInt(<span class="hljs-string">&#x27; 0xF&#x27;</span>, <span class="hljs-number">16</span>)); <span class="hljs-comment">//15</span><br></code></pre></td></tr></table></div></figure>
<h2 id="5-2-toString"><a href="#5-2-toString" class="headerlink" title="5.2 toString()"></a>5.2 toString()</h2><figure class="highlight arcade"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-8efx5olacl5vpl"></i><span>arcade</span><div class="collapse show" id="collapse-8efx5olacl5vpl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs arcade"><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(parseInt(<span class="hljs-string">&#x27;40&#x27;</span>).toString(<span class="hljs-number">16</span>)); <span class="hljs-comment">//28</span><br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-number">0x28</span>.toString(<span class="hljs-number">10</span>)); <span class="hljs-comment">//40  可以将任何进制转换成任何进制 缺陷就是输出的是字符串</span><br></code></pre></td></tr></table></div></figure>

<h1 id="6-手动实现base32"><a href="#6-手动实现base32" class="headerlink" title="6.手动实现base32"></a>6.手动实现base32</h1><figure class="highlight javascript"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-gvxdqmlacl5vpl"></i><span>javascript</span><div class="collapse show" id="collapse-gvxdqmlacl5vpl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">getBase64Code</span>(<span class="hljs-params"></span>)&#123; <span class="hljs-comment">// 获取 base64 编码集 A到Z a到z 0到9 再加上 +/</span><br>    <span class="hljs-keyword">const</span> <span class="hljs-variable constant_">AZ</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Array</span>(<span class="hljs-number">26</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-number">65</span>).<span class="hljs-title function_">map</span>(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> &#123;<br>      <span class="hljs-keyword">return</span> <span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCodePoint</span>(item + index);<br>      <span class="hljs-comment">// 返回 大写字母 A 到 Z  //65 是 A 的 unicode 编码</span><br>      <span class="hljs-comment">// fromCodePoint()函数 将unicode编码变成 字符</span><br>    &#125;).<span class="hljs-title function_">join</span>(<span class="hljs-string">&#x27;&#x27;</span>);<br><br>    <span class="hljs-keyword">const</span> az = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Array</span>(<span class="hljs-number">26</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-number">97</span>).<span class="hljs-title function_">map</span>(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> &#123;<br>      <span class="hljs-keyword">return</span> <span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCodePoint</span>(item + index);<br>    &#125;).<span class="hljs-title function_">join</span>(<span class="hljs-string">&#x27;&#x27;</span>); <span class="hljs-comment">// 返回 a 到 z</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-variable constant_">AZ</span> + az + <span class="hljs-string">&#x27;0123456789+/&#x27;</span>;  <span class="hljs-comment">// ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/  共64个字符</span><br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">base32</span>(<span class="hljs-params">str</span>)&#123;<br>    <span class="hljs-keyword">let</span> base64Code = <span class="hljs-title function_">getBase64Code</span>();<br>    <span class="hljs-keyword">let</span> bf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(str)<br>    <span class="hljs-keyword">let</span> newStr = <span class="hljs-string">&#x27;&#x27;</span>;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; bf.<span class="hljs-property">length</span>; i++) &#123;<br>        newStr += bf[i].<span class="hljs-title function_">toString</span>(<span class="hljs-number">2</span>)<br>    &#125;<br>    <span class="hljs-keyword">let</span> enCode = <span class="hljs-string">&#x27;&#x27;</span>;<br>    <span class="hljs-keyword">let</span> item = <span class="hljs-string">&#x27;&#x27;</span>; <br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; newStr.<span class="hljs-property">length</span>; i+= <span class="hljs-number">5</span>) &#123;<br>        item = <span class="hljs-string">&#x27;000&#x27;</span>+newStr.<span class="hljs-title function_">slice</span>(i, i + <span class="hljs-number">5</span>);<br>        enCode+=base64Code[<span class="hljs-built_in">parseInt</span>(item,<span class="hljs-number">2</span>)]<br>        <br>    &#125;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(enCode);<br>&#125;<br><span class="hljs-title function_">base32</span>(<span class="hljs-string">&#x27;马到成功&#x27;</span>)<br></code></pre></td></tr></table></div></figure>



<h1 id="7-JS大数相加"><a href="#7-JS大数相加" class="headerlink" title="7.JS大数相加"></a>7.JS大数相加</h1><ul>
<li>列竖式方法 从低位向最高位计算的<ol>
<li>把原始数字进行倒序</li>
<li>从个位起开始依次相加<figure class="highlight arcade"><i class="iconfont icon-top" type="button" data-toggle="collapse" data-target="#collapse-al7d83lacl5vpl"></i><span>arcade</span><div class="collapse show" id="collapse-al7d83lacl5vpl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs arcade">let numA = <span class="hljs-string">&quot;1234567890&quot;</span>,numB = <span class="hljs-string">&quot;123456789&quot;</span>;<br>let numAArray = numA.<span class="hljs-built_in">split</span>(<span class="hljs-string">&quot;&quot;</span>).<span class="hljs-built_in">map</span>(<span class="hljs-function">(<span class="hljs-params">item</span>) =&gt;</span> parseInt(item)).<span class="hljs-built_in">reverse</span>();<br>let numBArray = numB.<span class="hljs-built_in">split</span>(<span class="hljs-string">&quot;&quot;</span>).<span class="hljs-built_in">map</span>(<span class="hljs-function">(<span class="hljs-params">item</span>) =&gt;</span> parseInt(item)).<span class="hljs-built_in">reverse</span>();<br>let <span class="hljs-built_in">sum</span> = [].fill(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,(numA.<span class="hljs-built_in">length</span> &gt;= numB.<span class="hljs-built_in">length</span> ? numA.<span class="hljs-built_in">length</span> : numB.<span class="hljs-built_in">length</span>) + <span class="hljs-number">1</span>);<br><span class="hljs-keyword">for</span> (let i = <span class="hljs-number">0</span>; i &lt; numAArray.<span class="hljs-built_in">length</span>; i++) &#123;<br>  <span class="hljs-built_in">sum</span>[i] = numAArray[i];<br>&#125;<br>let up = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">for</span> (let i = <span class="hljs-number">0</span>; i &lt; numBArray.<span class="hljs-built_in">length</span>; i++) &#123;<br>  <span class="hljs-built_in">sum</span>[i] = <span class="hljs-built_in">sum</span>[i] + numBArray[i] + up;<br>  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">sum</span>[i] &gt; <span class="hljs-number">9</span>) &#123;<br>    <span class="hljs-built_in">sum</span>[i] = <span class="hljs-built_in">sum</span>[i] % <span class="hljs-number">10</span>;<br>    up = <span class="hljs-number">1</span>;<br>  &#125; <span class="hljs-keyword">else</span> &#123;<br>    up = <span class="hljs-number">0</span>;<br>  &#125;<br>&#125;<br><span class="hljs-keyword">if</span> (<span class="hljs-built_in">sum</span>[<span class="hljs-built_in">sum</span>.<span class="hljs-built_in">length</span> - <span class="hljs-number">1</span>] == <span class="hljs-number">0</span>) &#123;<span class="hljs-built_in">sum</span>.<span class="hljs-built_in">pop</span>()&#125;<br>let result = <span class="hljs-built_in">sum</span>.<span class="hljs-built_in">reverse</span>().join(<span class="hljs-string">&quot;&quot;</span>);<br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(<span class="hljs-built_in">Number</span>(numA) + <span class="hljs-built_in">Number</span>(numB));<br><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(result);<br></code></pre></td></tr></table></div></figure></li>
</ol>
</li>
</ul>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/" class="category-chain-item">前端基础</a>
  
  
    <span>></span>
    
  <a href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/JS/" class="category-chain-item">JS</a>
  
  
    <span>></span>
    
  <a href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/JS/%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2/" class="category-chain-item">进制转换</a>
  
  

  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/">#前端基础</a>
      
        <a href="/tags/js/">#js</a>
      
        <a href="/tags/%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2/">#进制转换</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>进制转换</div>
      <div>http://81.68.132.127/2022/10/20/JS/binary/二进制基础/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Ikki</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年10月20日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2022年10月20日</div>
        </div>
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/10/20/JS/binary/IEEE754%E6%A0%87%E5%87%86/" title="IEEE754标准">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">IEEE754标准</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/10/17/react/basic/react-all-api/" title="react-api">
                        <span class="hidden-mobile">react-api</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  <article id="comments" lazyload>
    
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"6KzYWc2oaEBaHgNl4UIicMJC-gzGzoHsz","appKey":"vJ4FIwKeHRmHraCJraAsjyZ7","path":"window.location.pathname","placeholder":"请输入你的评论\n昵称为必填项目(输入QQ号码可以自动获取昵称和邮箱)\n虽然email不是必选，但是填写了email可以收到推送通知哦！","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":true,"recordIP":true,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":true},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


  </article>


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
      IKKI-专业BUG开发
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="/fluid/js/star.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
